package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.*;
import com.ysd.lis.mapper.lab.LabInspectCompanyFeeitemMapper;
import com.ysd.lis.mapper.lab.LabInspectCompanyMapper;
import com.ysd.lis.mapper.lab.LabInspectCompanySalesmanMapper;
import com.ysd.lis.request.SysParamFieldValueDto;
import com.ysd.lis.request.SysParamParam;
import com.ysd.lis.request.lab.CompanyPageParam;
import com.ysd.lis.request.lab.InspectCompanyDto;
import com.ysd.lis.service.SysParamValueService;
import com.ysd.lis.service.lab.LabInspectCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.baomidou.mybatisplus.core.toolkit.StringUtils.*;

/**
 * <p>
 * 通道号管理表 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-10-12
 */
@Service
public class LabInspectCompanyServiceImpl extends ServiceImpl<LabInspectCompanyMapper, LabInspectCompany> implements LabInspectCompanyService {

    @Autowired
    LabInspectCompanyMapper labInspectCompanyMapper;

    @Autowired
    LabInspectCompanySalesmanMapper labInspectCompanySalesmanMapper;

    @Override
    public Result findInspectCompanyPageList(CompanyPageParam pageParam) {
        Page<LabInspectCompany> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        LambdaQueryWrapper<LabInspectCompany> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LabInspectCompany::getDelFlag, 0);
        if (isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(LabInspectCompany::getCompanyName, pageParam.getSeachValue())
                    .or().like(LabInspectCompany::getCompanyCode, pageParam.getSeachValue())
                    .or().like(LabInspectCompany::getAlias, pageParam.getSeachValue())
                    .or().like(LabInspectCompany::getInputCode, pageParam.getSeachValue()));
        }
        lambdaQueryWrapper.orderByAsc(LabInspectCompany::getSeq);
        Page<LabInspectCompany> supplierPage = labInspectCompanyMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }

    @Override
    public Result batchDeleteInspectCompanys(InspectCompanyDto dto) {
        List<LabInspectCompany> inspectCompanyList = dto.getInspectCompanyList();

        List<String> ids = inspectCompanyList.stream().map(LabInspectCompany::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result addInspectCompany(LabInspectCompany company) {
        int insert = labInspectCompanyMapper.insert(company);
        if (insert > 0) {
            return Result.succ(1, "添加成功", company);
        } else {
            return Result.fail(400, "添加失败", "");
        }
    }

    @Override
    public Result editInspectCompany(LabInspectCompany company) {
        int updateById = labInspectCompanyMapper.updateById(company);
        if (updateById > 0) {
            return Result.succ(1, "提交成功", company);
        } else {
            return Result.fail(400, "修改失败", "");
        }
    }

    @Override
    public Result findInspectCompanyList(CompanyPageParam pageParam) {
        LambdaQueryWrapper<LabInspectCompany> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LabInspectCompany::getDelFlag, 0);
        if (isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(LabInspectCompany::getCompanyName, pageParam.getSeachValue())
                    .or().like(LabInspectCompany::getCompanyCode, pageParam.getSeachValue())
                    .or().like(LabInspectCompany::getAlias, pageParam.getSeachValue())
                    .or().like(LabInspectCompany::getInputCode, pageParam.getSeachValue()));
        }
        lambdaQueryWrapper.orderByAsc(LabInspectCompany::getSeq);
        List<LabInspectCompany> supplierPage = labInspectCompanyMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }

    @Autowired
    SysParamValueService sysParamValueService;

    @Override
    public Result findInspectCompanysByPendSettle(CompanyPageParam pageParam) {
        SysParamParam sysp1 = new SysParamParam();
        sysp1.setParamCode("xtxxcs");
        sysp1.setField("tmcwjsjd");
        Integer state = 105;
        try {
            List<SysParamFieldValueDto> r1 = sysParamValueService.findSysParamFieldAndValueListByCode(sysp1);
            state = Integer.valueOf(r1.get(0).getValue());

        } catch (Exception e) {
            state = 105;
        }


        MPJLambdaWrapper<LabInspectCompany> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        //Integer finalState = state;
        mpjLambdaWrapper.selectAll(LabInspectCompany.class)
                //.innerJoin(LabReqMain.class, p -> p.eq(LabReqMain::getInspectCompany, LabInspectCompany::getCompanyCode).ge(LabReqMain::getState, finalState).and(wrapper -> wrapper.isNull(LabReqMain::getIsSettle).or().ne(LabReqMain::getIsSettle, 1)).eq(LabReqMain::getDelFlag, 0))
                .eq(LabInspectCompany::getDelFlag, 0)
                .exists(" SELECT 1 FROM LAB_REQ_MAIN T1 WHERE t1.STATE >= " + state + " AND t1.INSPECT_COMPANY = t.COMPANY_CODE AND ( t1.IS_SETTLE IS NULL OR t1.IS_SETTLE <> 1 ) AND t1.del_flag = 0");


        if (isNotBlank(pageParam.getSeachValue())) {
            mpjLambdaWrapper.and(wrapper -> wrapper.like(LabInspectCompany::getCompanyName, pageParam.getSeachValue())
                    .or().like(LabInspectCompany::getCompanyCode, pageParam.getSeachValue())
                    .or().like(LabInspectCompany::getAlias, pageParam.getSeachValue())
                    .or().like(LabInspectCompany::getInputCode, pageParam.getSeachValue()));
        }
        mpjLambdaWrapper.orderByAsc(LabInspectCompany::getSeq);
        List<LabInspectCompany> supplierPage = labInspectCompanyMapper.selectList(mpjLambdaWrapper);
        if (CollectionUtils.isNotEmpty(supplierPage)){
            List<String> companyCodeList = supplierPage.stream().map(LabInspectCompany::getCompanyCode).collect(Collectors.toList());
            List<LabInspectCompanySalesman> companySalesmanList = labInspectCompanySalesmanMapper.selectList(new LambdaQueryWrapper<LabInspectCompanySalesman>().in(LabInspectCompanySalesman::getCompanyCode, companyCodeList).eq(LabInspectCompanySalesman::getDelFlag, 0));
            Map<String, List<LabInspectCompanySalesman>> feeitemMap = companySalesmanList.stream().collect(Collectors.groupingBy(LabInspectCompanySalesman::getCompanyCode));
            for (LabInspectCompany labInspectCompany : supplierPage) {
                List<LabInspectCompanySalesman> companyFeeitemList1 = feeitemMap.get(labInspectCompany.getCompanyCode());
                if (CollectionUtils.isNotEmpty(companyFeeitemList1)){
                    labInspectCompany.setSalesmanCode(companyFeeitemList1.get(0).getCode());
                    labInspectCompany.setSalesmanName(companyFeeitemList1.get(0).getName());
                }
            }
        }

        return Result.succ(1, "查询成功", supplierPage);
    }

    @Override
    public Result findCompanyBySalesmanList(CompanyPageParam pageParam) {
        MPJLambdaWrapper<LabInspectCompany> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(LabInspectCompany::getDelFlag, 0);
        if (isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(LabInspectCompany::getCompanyCode, pageParam.getSeachValue())
                    .or().like(LabInspectCompany::getCompanyName, pageParam.getSeachValue())
                    .or().like(LabInspectCompany::getInputCode, pageParam.getSeachValue()));
        }
        lambdaQueryWrapper.notExists("SELECT 1 FROM LAB_INSPECT_COMPANY_SALESMAN t2 WHERE t2.DEL_FLAG=0 and t.COMPANY_CODE = t2.COMPANY_CODE ");
        lambdaQueryWrapper.orderByAsc(LabInspectCompany::getSeq);
        List<LabInspectCompany> selectList = labInspectCompanyMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", selectList);
    }
}
